/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 10/20/2010
 * Time: 3:12 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;

namespace MediafireDownloader
{
	/// <summary>
	/// Description of URLValidator.
	/// </summary>
	public partial class URLValidator : Form
	{
		
		public string Filename;
		public string FileSize;
		public string Result = "Cancel";
		bool DocumentCompleted = false;
		
		public URLValidator(string url)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			webBrowser1.ScriptErrorsSuppressed = true;			
			webBrowser1.Url = new Uri(url);
			
			if(DocumentCompleted) DocumentCompleted = false;
			
			timeoutTimer.Start();
			validateTimer.Start();
		}
			
		void TimeoutTimerTick(object sender, EventArgs e)
		{
			timeoutTimer.Stop();
			validateTimer.Stop();
			Result = "Request Timeout";
			this.Close();
		}
		
		void ValidateTimerTick(object sender, EventArgs e)
		{
			validateTimer.Stop();
			
			try{
				WindowLib.killProcess("IEXPLORE");
			}catch{}
			
//			if( !DocumentCompleted ){
//				validateTimer.Start();
//				return;
//			}
			
			GetURL();
			if( Result == "OK" || Result == "Invalid/Deleted" ){
				timeoutTimer.Stop();
				validateTimer.Stop();
				this.Close();
			}
			
			validateTimer.Start();
		}
		
		void GetURL(){
			try{
				
				//Check if error message - no longer existed
				HtmlElement msgElement = webBrowser1.Document.GetElementById("statusmessage_text");
				if( msgElement != null && msgElement.InnerHtml.Contains("The content no longer seems to be available via MediaFire")){
					Result = "Invalid/Deleted";
					return;
				}
			}catch{}
			
			try{
				//Check if error message - invalid/delete
				HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("div");
				foreach( HtmlElement element in elements ){
					try{
						if( element.InnerText.Contains("Invalid or Deleted File") ||
						     element.InnerHtml.Contains("Invalid or Deleted File")
						   ){
								Result = "Invalid/Deleted";
								return;
						}
					}catch{}
				}			
			}catch{}
				
			try{
				//Check if file exit
				HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("div");
				string[] parts;
				foreach( HtmlElement element in elements ){
					if( element.GetAttribute("className")=="download_file_title" ){							
						parts = StringLib.splitByAny( element.InnerText, new string[]{"\r\n"} );
						Filename = parts[0];
						FileSize = parts[1].Replace("(","").Replace(")","").Trim();
						Result = "OK";
						return;
					}
				}
			}catch{}
		}	
		
		void WebBrowser1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			DocumentCompleted = true;
		}
	}
}
